const {
  CurrencyShopModel
} = require('../../../utils/currencyshop-model.js')
const {
  GamesModel
} = require('../../../utils/games-model.js')
const {
  MemberModel
} = require('../../../utils/member-model.js');
var memberModel = new MemberModel();
const currencyShopModel = new CurrencyShopModel()
const gamesModel = new GamesModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderCode: "",
    shopId: "",
    orderInfo: {},
    redPacketShow: false,
    isRedPacket: false, //是否有分享活动
    themeUrL: "",
    activityInfo: {},
    memberId: "",
    shareImage: "https://oss.kiscloud.net/wechat/images/share_red_packet.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.getUserInfo();
    that.setData({
      orderCode: options.orderCode,
      shopId: options.shopId,
    }, function () {
      that.getOrderQueryDetail(); //订单详情
      that.getActivityList(); //分享活动
    })
  },
  /**
   *微信会员信息
   */
  getUserInfo: function () {
    memberModel.userInfo({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      var that = this;
      that.setData({
        memberId: data.memberId || "", //会员ID
      });
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 订单详情
   */
  getOrderQueryDetail: function () {
    currencyShopModel.orderQueryDetail({
      orderCode: this.data.orderCode, //订单号
      shopId: this.data.shopId, //门店id
      merchantId: wx.getStorageSync('merchantId') //商户id
    }).then(data => {
      this.setData({
        orderInfo: data || {}
      });
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 参与分享活动
   */
  getActivityList: function () {
    gamesModel.activityList({
      merchantId: wx.getStorageSync('merchantId'), //商户id
      activityType: 11, //分享活动
      activityStatus: 1 //已开始
    }).then(data => {
      if (data && data.length > 0) {
        this.setData({
          isRedPacket: true,
          redPacketShow: true,
          activityInfo: data[0] || {}
        });
      } else {
        this.setData({
          redPacketShow: false,
          isRedPacket: false
        });
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 显示红包弹出层
   */
  popupRedPacket: function () {
    this.setData({
      redPacketShow: !this.data.redPacketShow
    })
  },

  /**
   * 跳转到首页
   */
  goHome: function () {
    wx.reLaunch({
      url: '/pages/memberCenter/index/index'
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    this.setData({
      redPacketShow: false
    })
    return {
      title: this.data.activityInfo.shareTitle || "",
      imageUrl: this.data.activityInfo.shareImage ? this.data.activityInfo.shareImage : this.data.shareImage,
      path: `pages/games/redPacket/index?shareMemberId=${this.data.memberId}&activityId=${this.data.activityInfo.activityId}&activityName=${this.data.activityInfo.activityName}&orderCode=${this.data.orderCode}&isShare=1`,
    }
  }
})